//
//  JWDoubleTextPickerManager.swift
//  oa
//
//  Created by lin on 2022/3/15.
//

import Foundation
import UIKit

public protocol PickerTextProtocol {
    /// 唯一标识符
    var pickerId: String { get }
    /// 标题
    var pickerText: String { get }
    /// 子项
    var pickerItemList: [PickerTextProtocol] { get }
    
}

extension PickerTextProtocol {
    
    public var pickerId: String { return "" }
    
    public var pickerText: String { return "" }
    
    public var pickerItemList: [PickerTextProtocol] { return [] }
    
}

public class PickerTextModel: NSObject, PickerTextProtocol {
    
    public var pickerId: String = ""

    public var pickerText: String = ""
    
    public var pickerItemList: [PickerTextProtocol] = []
}


/// 列类型(即列数，最多列)
public enum JWTextPickerColumnsType: Int {
    
    case one = 1
    case two = 2
    case three = 3
    
    var columnCount: Int { return self.rawValue }
}

/// 多文本选择器弹窗
public class JWTextPickerManager: NSObject {
    
    /// 取消CancelHandler
    public typealias CancelHandler = (_ contentView: JWPickerViewController) -> Void
    
    /// 确定ConfirmHandler
    public typealias ConfirmHandler = (_ contentView: JWPickerViewController, _ result: (firstModel: PickerTextProtocol, secondModel: PickerTextProtocol?, threeModel: PickerTextProtocol?)) -> Void
    
    /// 取消回掉
    public var cancelCallBack: CancelHandler?
    
    /// 确定回掉
    public var confirmCallBack: ConfirmHandler?
    
    /// 数据源
    private let dataSources: [PickerTextProtocol]
    
    /// 头部标题
    public var headerTitle: String
    
    /// 选中索引
    public var selectedIndexs: (firstIndex: Int, secondIndex: Int, thirdIndex: Int) = (firstIndex: 0, secondIndex: 0, thirdIndex: 0)
    
    /// 类型
    private let type: JWTextPickerColumnsType
    
    public init(title: String = "", type: JWTextPickerColumnsType = .one, dataSources: [PickerTextProtocol]) {
        self.dataSources = dataSources
        self.headerTitle = title
        self.type = type
        super.init()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension JWTextPickerManager: JWPickerViewControllerDataSource {
    
    /// 设置标题富文本
    public var title: NSAttributedString? {
        return NSAttributedString(string: headerTitle, attributes: [.font : UIFont.systemFont(ofSize: 16), .foregroundColor: UIColor(red: 0, green: 0, blue: 0, alpha: 1)])
    }
    
    public var unitAttributedText: NSAttributedString? { return nil }
    
    /// 设置默认选中
    public func pickerDefaultSelected(_ contentView: JWPickerViewController) {
        if numberOfComponents(in: contentView) > 0 {
            if pickerContentView(contentView, numberOfRowsInComponent: 0) > self.selectedIndexs.firstIndex {
                contentView.selectRow(self.selectedIndexs.firstIndex, inComponent: 0, animated: true)
            } else {
                contentView.selectRow(0, inComponent: 0, animated: true)
            }
        }
        if numberOfComponents(in: contentView) > 1 {
            if pickerContentView(contentView, numberOfRowsInComponent: 1) > self.selectedIndexs.secondIndex {
                contentView.selectRow(self.selectedIndexs.secondIndex, inComponent:1, animated: true)
            } else {
                contentView.selectRow(0, inComponent: 1, animated: true)
            }
        }
        if numberOfComponents(in: contentView) > 2 {
            if pickerContentView(contentView, numberOfRowsInComponent: 2) > self.selectedIndexs.thirdIndex {
                contentView.selectRow(self.selectedIndexs.thirdIndex, inComponent:2, animated: true)
            } else {
                contentView.selectRow(0, inComponent: 2, animated: true)
            }
        }
    }
    
    public func pickerContentView(_ contentView: JWPickerViewController, widthForComponent component: Int) -> CGFloat {
        switch self.type {
        case .one:
            return contentView.view.bounds.size.width - 20
        case .two:
            return (contentView.view.bounds.size.width - 20 - 10) / 2
        case .three:
            return (contentView.view.bounds.size.width - 20 - 20) / 3
        }
    }
    public func pickerContentView(_ contentView: JWPickerViewController, titleForRow row: Int, forComponent component: Int) -> String {
        var text: String = ""
        switch component {
        case 0:
            text = self.dataSources[row].pickerText
        case 1:
            let firstIndex = self.selectedIndexs.firstIndex
            /// 第一层模型
            let firstModel = self.dataSources[min(firstIndex, self.dataSources.count - 1)]
            if firstModel.pickerItemList.count > row {
                /// 第二层模型
                let secondModel = firstModel.pickerItemList[row]
                text = secondModel.pickerText
            }
            
        case 2:
            let firstIndex = self.selectedIndexs.firstIndex
            /// 第一层模型
            let firstModel = self.dataSources[min(firstIndex, self.dataSources.count - 1)]
            /// 第二层模型
            let secondIndex = self.selectedIndexs.secondIndex
            let secondModel = firstModel.pickerItemList[min(secondIndex, firstModel.pickerItemList.count - 1)]
            
            if secondModel.pickerItemList.count > row {
                /// 第三层模型
                let threeModel = secondModel.pickerItemList[row]
                text = threeModel.pickerText
            }

        default:
            break
        }
        return text
    }
    
    public func pickerContentView(_ contentView: JWPickerViewController, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return self.dataSources.count
        case 1:
            let firstIndex = self.selectedIndexs.firstIndex
            if self.dataSources.count > firstIndex {
                return self.dataSources[firstIndex].pickerItemList.count
            }
            return 0
        case 2:
            let firstIndex = self.selectedIndexs.firstIndex
            let secondIndex = self.selectedIndexs.secondIndex
            if self.dataSources.count > firstIndex, self.dataSources[firstIndex].pickerItemList.count > secondIndex {
                return self.dataSources[firstIndex].pickerItemList[secondIndex].pickerItemList.count
            }
            return 0
        default:
            return 0
        }
    }
    
    public func numberOfComponents(in contentView: JWPickerViewController) -> Int {
        return self.type.columnCount
    }
    
    
}
extension JWTextPickerManager: JWPickerViewControllerDelegate {
    
    public func pickerContentView(_ contentView: JWPickerViewController, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            self.selectedIndexs.firstIndex = row
            if numberOfComponents(in: contentView) > 1 {
                contentView.reloadComponent(1)
                contentView.selectRow(0, inComponent: 1, animated: true)
                self.selectedIndexs.secondIndex = 0
                self.selectedIndexs.thirdIndex = 0
            }
            
            if numberOfComponents(in: contentView) > 2 {
                contentView.reloadComponent(2)
                contentView.selectRow(0, inComponent: 2, animated: true)
                self.selectedIndexs.secondIndex = 0
                self.selectedIndexs.thirdIndex = 0
            }
            
        case 1:
            self.selectedIndexs.secondIndex = row
            if numberOfComponents(in: contentView) > 2 {
                contentView.reloadComponent(2)
                contentView.selectRow(0, inComponent: 2, animated: true)
                self.selectedIndexs.thirdIndex = 0
            }
        case 2:
            self.selectedIndexs.thirdIndex = row
        default:
            break
        }
    }
    
    public func pickerConfirm(_ contentView: JWPickerViewController) {
        if let confirmCallBack = confirmCallBack {
            let firstIndex = self.selectedIndexs.firstIndex
            let secondIndex = self.selectedIndexs.secondIndex
            let threeIndex = self.selectedIndexs.thirdIndex
            let firstModel = self.dataSources[firstIndex]
            var secondModel: PickerTextProtocol? = nil
            var threeModel: PickerTextProtocol? = nil
            if (self.dataSources[firstIndex].pickerItemList.count > secondIndex) {
                secondModel =  self.dataSources[firstIndex].pickerItemList[secondIndex]
                if let secondModel = secondModel, secondModel.pickerItemList.count > threeIndex {
                    threeModel = secondModel.pickerItemList[threeIndex]
                }
            }
            confirmCallBack(contentView, (firstModel: firstModel, secondModel: secondModel, threeModel: threeModel))
        }
    }
    
    public func pickerCancel(_ contentView: JWPickerViewController) {
        if let cancelCallBack = cancelCallBack {
            cancelCallBack(contentView)
        }
        
    }
    
    
}



